import React, { useState, useEffect } from 'react';
import {
    Button,
    Cascader,
    DatePicker,
    Form,
    Input,
    InputNumber,
    Radio,
    Select,
    Switch,
    TreeSelect,
} from 'antd';
import { useAppDispatch, useAppSelector } from '../../store/reactreduxts';
import type { shopTS, typeTS } from '../../store/crmsTS';
import type { FormProps } from 'antd';
import * as async from '../../store/crmsSlice'

interface Iprops {
    title: string,
    itemobj: {} | shopTS,
    handleOk: () => void
}

const Myfrom: React.FC<Iprops> = (props) => {

    //获取数据
    let obj = useAppSelector(state => state.crms1)
    let dispatch = useAppDispatch()

    //编辑数据回显
    const [form] = Form.useForm();
    useEffect(() => {
        form.setFieldsValue(props.itemobj)
    }, [])

    const onFinish: FormProps<shopTS>['onFinish'] = (values) => {
        //规格判断
        let specification = values.speciType == 1 ? ["全糖", "半糖", "无糖"] : ["加冰", "少冰", "不加冰"]
        if (props.title == '添加') {
            //添加的方法
            let obj = {
                ...values,
                "specification": specification,
                "id": new Date().getTime()
            }
            dispatch(async.async_addshoplist(obj))
        } else {
            //编辑的方法
            let obj = {
                ...values,
                "specification": specification,
                "id": props.itemobj.id
            }
            dispatch(async.async_editshoplist(obj))
        }
        //方法完成后 隐藏弹框
        props.handleOk()
    };

    return (
        <Form onFinish={onFinish} form={form}>

            <Form.Item label="商品名称" name='title'
                rules={[{ required: true, message: 'Please input your username!' }]}>
                <Input />
            </Form.Item>

            <Form.Item label="商品类型" name='typeId'
                rules={[{ required: true, message: 'Please input your username!' }]}>
                <Select>
                    {
                        obj.typelist.map((item: typeTS) => {
                            return <Select.Option key={item.id} value={item.typeId}>
                                {item.label}
                            </Select.Option>
                        })
                    }

                </Select>
            </Form.Item>

            <Form.Item label="商品价格" name='price'
                rules={[{ required: true, message: 'Please input your username!' }]}>
                <InputNumber />
            </Form.Item>

            <Form.Item label="商品状态" name="Collect">
                <Switch />
            </Form.Item>

            <Form.Item label="商品规格" name="speciType"
                rules={[{ required: true, message: 'Please input your username!' }]}>
                <Radio.Group
                    options={[
                        { value: 1, label: '可选糖' },
                        { value: 2, label: '可选冰' },
                    ]}
                />
            </Form.Item>

            <Form.Item>
                <Button type='primary' htmlType="submit">完成</Button>
            </Form.Item>
        </Form>
    );
};

export default Myfrom;